package com.ruoyi.aitcommon.api;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.aitcommon.anotation.ApiLog;
import com.ruoyi.aitcommon.utils.JSONResult;
import com.ruoyi.aitcommon.utils.JSONUtils;
import com.ruoyi.aitcommon.utils.MapUtils;
import com.ruoyi.aitcommon.utils.WebServiceUtils;

/**
 * WebService API
 */
@RestController
@RequestMapping("/ait/common/wsoper")
public class WSOperation {
	
	/**
	 * <pre>
	 * WebService通用接口
	 * jsonstr格式：
{
	"data": {
		"parm": "Webservice的入口参数"
	},
	"context": {
		"endpoint": "http://localhost/uapws/service/nc.ymyy.ws.ISXDataSync",
		"func": "hello"
	}
}
	 * 
	 * </pre>
	 * @param jsonstr
	 * @return
	 */
	@ApiLog("WebService通用接口")
	@PostMapping("/invoke")
	public JSONObject commonInvoke(@RequestBody String jsonstr) {
		try {
			if (StringUtils.isBlank(jsonstr))
				throw new Exception("传入参数为空");
			
			JSONObject json=JSONUtils.parseObject(jsonstr);
			String endpoint=MapUtils.getPathString("context.endpoint", json);
			String func=MapUtils.getPathString("context.func", json);
			String parm=MapUtils.getPathString("data.parm", json);
			String ret=WebServiceUtils.invokeRemote(endpoint, func, parm);
			return JSONResult.success("执行成功",ret);
		}catch(Exception ex) {
			return JSONResult.error(ex.getMessage());
		}
	}
}
